﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Model;

namespace BusinessLogic
{
    public class GetCarAdvanceCommand : BaseCommand<List<acm_car_advance>>
    {
        private bool isCount { get; set; }
        public int ReccordCount { get; set; }

        public GetCarAdvanceCommand(string VIN)
        {   //Check VIN
            SetQuery("select id,vin,car_series,car_sub_series,car_model_suffix,sale_date,user_id,dealer_code,tx_date, 1 rec_no from acm_car_advance where vin='" + VIN + "' and approve = 1");
            isCount = false;
        }

        public GetCarAdvanceCommand(string vin, string srt, string end, string dealerCode)
        {
            //Count
            SetQuery(String.Format("exec ACM_CAR_ADV_SEARCH '{0}',{1},{2},'{3}',null,null", vin, srt, end, dealerCode));
            this.isCount = true;
        }

        public GetCarAdvanceCommand(string vin,string srt, string end,string dealerCode, string pageId,string rowPerPage)
        {
            //Search
            SetQuery(String.Format("exec ACM_CAR_ADV_SEARCH '{0}',{1},{2},'{3}',{4},{5}",vin,srt,end,dealerCode,pageId,rowPerPage));
            this.isCount = false;
        }

        public override void Command()
        {
            Result = new List<acm_car_advance>();
            SqlDataReader dr = cmd.ExecuteReader();
            
            if (isCount)
            {
                dr.Read();
                if (dr.HasRows)
                {
                    ReccordCount = dr.GetInt32(0);
                }
            }
            else
            {
                while (dr.Read())
                {
                    acm_car_advance row = new acm_car_advance();
                    row.id = GetNullableInt(dr, 0);
                    row.vin = GetNullableString(dr, 1);
                    row.car_series = GetNullableString(dr, 2);
                    row.car_sub_series = GetNullableString(dr, 3);
                    row.car_model_suffix = GetNullableString(dr, 4);
                    row.sale_date = GetNullableDateTime(dr, 5).Value;
                    row.user_id = GetNullableInt(dr, 6);
                    row.dealer_code = GetNullableString(dr, 7);
                    row.tx_date = GetNullableDateTime(dr, 8);
                    row.rec_no = GetNullableInt(dr, 9);

                    Result.Add(row);
                }
            }
        }
    }
}
